package pl.register.android.database.crud;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import pl.register.android.database.exception.NotFoundCourseException;
import pl.register.android.course.Course;
import pl.register.android.database.dao.CourseDao;
import pl.register.android.database.tables.CourseDBTable;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-26
 */
public class CourseCRUD implements CourseDao {

    private SQLiteDatabase database;

    public CourseCRUD(SQLiteDatabase database) {
        this.database = database;
    }

    @Override
    public Course findCourseBy(Long courseId) {
        Course course = null;
        String where = CourseDBTable.ID + " = " + courseId;
        Cursor cursor = database.query(CourseDBTable.TABLE_NAME, null, where, null, null, null, null);
        if (cursor.moveToFirst()) {
            int courseStartYear = cursor.getInt(cursor.getColumnIndex(CourseDBTable.START_YEAR));
            int courseEndYear = cursor.getInt(cursor.getColumnIndex(CourseDBTable.END_YEAR));
            course = new Course(courseId, courseStartYear, courseEndYear);
        }
        cursor.close();
        return course;
    }

    @Override
    public Course getLastCourse() {
        Course course = null;
        String orderBy =  CourseDBTable.ID + " DESC";
        Cursor cursor = database.query(CourseDBTable.TABLE_NAME, null, null, null, null, null, orderBy);
        if (cursor.moveToFirst()) {
            Long courseId = cursor.getLong(cursor.getColumnIndex(CourseDBTable.ID));
            int courseStartYear = cursor.getInt(cursor.getColumnIndex(CourseDBTable.START_YEAR));
            int courseEndYear = cursor.getInt(cursor.getColumnIndex(CourseDBTable.END_YEAR));
            course = new Course(courseId, courseStartYear, courseEndYear);
        }
        cursor.close();
        return course;
    }

    @Override
    public Course findCourseFor(int startYear, int endYear) throws NotFoundCourseException {
        Course course = null;
        String where = CourseDBTable.START_YEAR + " = " + startYear + " AND " + CourseDBTable.END_YEAR + " = " + endYear;
        Cursor cursor = database.query(CourseDBTable.TABLE_NAME, null, where, null, null, null, null);
        if (cursor.moveToFirst()) {
            Long courseId = cursor.getLong(cursor.getColumnIndex(CourseDBTable.ID));
            int courseStartYear = cursor.getInt(cursor.getColumnIndex(CourseDBTable.START_YEAR));
            int courseEndYear = cursor.getInt(cursor.getColumnIndex(CourseDBTable.END_YEAR));
            course = new Course(courseId, courseStartYear, courseEndYear);
        }
        cursor.close();
        if(course != null){
            return course;
        } else {
            throw new NotFoundCourseException();
        }
    }

    @Override
    public Course insert(Course course) {
        ContentValues insertValues = new ContentValues();
        insertValues.put(CourseDBTable.START_YEAR, course.getStartYear());
        insertValues.put(CourseDBTable.END_YEAR, course.getEndYear());

        long rowId = database.insert(CourseDBTable.TABLE_NAME, null, insertValues);
        course.setId(rowId);
        return course;
    }

}